package com.songpeng.provider.demo.service;


//import business.model.Order;

import business.service.AccountService;
import business.service.OrderService;
import com.songpeng.provider.demo.dao.OrderDao;
import com.songpeng.provider.demo.model.Order;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @DubboReference
    private AccountService accountService;

    @Override
    public int create(String userId, String commodityCode, int orderCount) {
        int orderMoney = calculate(commodityCode, orderCount);

        accountService.debit(userId, orderMoney);

        Order order = new Order();
        order.setUserId( userId );
        order.setCommodityCode( commodityCode );
        order.setCount( orderCount );
        order.setMoney( orderMoney );

        // INSERT INTO orders ...
        return orderDao.insert(order);
    }

    /**
     * 默认5元一件（个）
     * @param commodityCode
     * @param orderCount
     * @return
     */
    private int calculate(String commodityCode, int orderCount) {
        return orderCount * 5;
    }
}
